﻿using UnityEngine;
using System.Collections;

public class UnitShadow : MonoBehaviour {
    bool m_bShadowAlphaToZero;
    bool m_bShadowAlphaToOne;
	// Use this for initialization
	void Start () {
        GetComponent<SpriteRenderer>().color = new Color(1, 1, 1, 100 / 255f);
	}
	
	// Update is called once per frame
	void Update () {
        transform.localPosition = transform.parent.transform.FindChild("Main").FindChild("Unit").transform.localPosition;

        if (Clock.iHour == 5)
            m_bShadowAlphaToOne = true;

        if (Clock.iHour >= 5 && Clock.iHour <= 20)
            transform.localPosition = transform.localPosition - Vector3.Normalize(GameObject.Find("SUN").transform.position - transform.parent.FindChild("Main").FindChild("Unit").position) * 0.05f;
        else
        {
            GetComponent<SpriteRenderer>().color = new Color(1, 1, 1, 0);
            m_bShadowAlphaToZero = false;
        }

        if (Clock.iHour == 17)
            m_bShadowAlphaToZero = true;

        if(m_bShadowAlphaToZero)
            GetComponent<SpriteRenderer>().color = new Color(1, 1, 1, GetComponent<SpriteRenderer>().color.a - (0.25f * Time.deltaTime));
        else if(m_bShadowAlphaToOne)
            if (GetComponent<SpriteRenderer>().color.a < 100/255f)
                GetComponent<SpriteRenderer>().color = new Color(1, 1, 1, GetComponent<SpriteRenderer>().color.a + (0.25f * Time.deltaTime));
            else
            {
                GetComponent<SpriteRenderer>().color = new Color(1, 1, 1, 100 / 255f);
                m_bShadowAlphaToOne = false;
            }
	}
}
